﻿-- Author	: Kurapica

--------------------------
-- Marker               --
--------------------------
do
	local getmetatable = getmetatable
	local pairs = pairs
	local ipairs = ipairs
	local type = type
	local error = error
	local tinsert = table.insert
	local tremove = table.remove
	local CreateFrame = IGAS.GUI.CreateFrame

	-- Base Form for all other forms in Studio    
	local _WidgetName = "Marker"
	local _Base = IGAS.GUI.Widget.VirtualUIObject

    local _Widget = IGAS.Studio.Widget
    local _PropertyType = IGAS.GUI.Base.PropertyType

	-- Only one Marker will be created.
    local marker
	local mask, t
	
	-- Script
	local _Dir = {
		"TOP",
		"BOTTOM",
		"LEFT",
		"RIGHT",
		"TOPLEFT",
		"TOPRIGHT",
		"BOTTOMLEFT",
		"BOTTOMRIGHT",
	}
    
	-- Dirs's Scripts
	local function DirOnMouseDown(self)
		if marker.InMarkMode and marker.MarkedFrame then
			marker.__MarkedFrame:StartSizing(self.__Dir)
		end
	end
	
	local function DirOnMouseUp(self)
		if marker.InMarkMode and marker.MarkedFrame then
			marker.__MarkedFrame:StopMovingOrSizing()
			marker.__MarkedFrame:ClearAllPoints()
			marker.__MarkedFrame:SetPoint("TOPLEFT", marker.__MarkedFrame.Parent, marker.__MarkedFrame:GetLeft() - marker.__MarkedFrame.Parent:GetLeft(), marker.__MarkedFrame:GetTop() - marker.__MarkedFrame.Parent:GetTop())
		end
	end
   
	-- Hook Frame Scripts	
	local function frameOnMouseUp(self, ...)		
		if marker.InMarkMode and self == marker.MarkedFrame then
			self:StopMovingOrSizing()
			self:ClearAllPoints()
			self:SetPoint("TOPLEFT", self.Parent, self:GetLeft() - self.Parent:GetLeft(), self:GetTop() - self.Parent:GetTop())
		end
	end
	
	local function frameOnMouseDown(self, ...)
		marker.MarkedFrame = self
		
		if marker.InMarkMode and self == marker.MarkedFrame then
			self:StartMoving()
		end
	end
	
	local function frameOnEnter(self)
		if marker.InMarkMode and self ~= marker.MarkedFrame then
			self.IGAS_STUDIO_MARKER.MaskColor.Visible = true
		end
	end
	
	local function frameOnLeave(self)
		if marker.InMarkMode and self ~= marker.MarkedFrame then
			self.IGAS_STUDIO_MARKER.MaskColor.Visible = false
		end
	end
    
	-- ScriptType
	local _ScriptType = {
		["OnSelected"] = true,
	}

	-- FuncProxy
	local _FuncProxy = {
		-- HookFrame
		["HookFrame"] = function(self, frame)			
			mask = CreateFrame("Frame", "IGAS_STUDIO_MARKER", frame)
			
			mask:SetAllPoints(frame)
			mask.Visible = true
			mask.TopLevel = true
			
			t = CreateFrame("Texture", "MaskColor", mask)
			t:SetTexture(0.3, 0.1, 0.4, 0.5)
			t:SetAllPoints(mask)
			t.Visible = false
		   
			for _, d in ipairs(_Dir) do
				dir = CreateFrame("Frame", "Marker_"..d, mask)
				dir.__Dir = d
				dir.Width = 12
				dir.Height = 12
				dir.TopLevel = true
				dir.Visible = false
				dir.MouseEnabled = true			
				dir.OnMouseUp = DirOnMouseUp
				dir.OnMouseDown = DirOnMouseDown
			   
				t = CreateFrame("Texture", "Back", dir, "BACKGROUND")
				t:SetTexture("Interface\\Buttons\\UI-CheckBox-Up.blp")
				t:SetAllPoints(dir)
				dir:SetPoint("CENTER", mask, d, 0, 0)
			end
			
			frame.__UI:EnableMouse(true)
			frame.__UI:SetMovable(true)
			frame.__UI:SetResizable(true)
			frame:SetMinResize(10, 10)
			
            frame:StackScript("OnMouseUp", frameOnMouseUp)
			frame:StackScript("OnMouseDown", frameOnMouseDown)
			frame:StackScript("OnEnter", frameOnEnter)
			frame:StackScript("OnLeave", frameOnLeave)
		end,
	}

	-- Property
	local _Property = {
		-- InMarkMode
		["InMarkMode"] = {
			["Set"] = function(self, flag)
				self.__InMarkMode = (flag and true) or false
				if not self.__InMarkMode and self.__MarkedFrame then
					self.__MarkedFrame.IGAS_STUDIO_MARKER.Visible = false
					self.__MarkedFrame = nil
					self:Fire("OnSelected", nil)
				end
			end,
			
			["Get"] = function(self)
				return (self.__InMarkMode and true) or false
			end,
			
			["Type"] = _PropertyType.Boolean,
		},
		-- MarkedFrame
		["MarkedFrame"] = {
			["Set"] = function(self, frame)
				if not self.__InMarkMode then
					return
				end
				if frame and type(frame) == "table" and frame.IsIGASWidget then
					if self.__MarkedFrame ~= frame then
						if self.__MarkedFrame then
							self.__MarkedFrame.IGAS_STUDIO_MARKER.MaskColor.Visible = false
							for _, d in ipairs(_Dir) do
								self.__MarkedFrame.IGAS_STUDIO_MARKER["Marker_"..d].Visible = false
							end
						end
						self.__MarkedFrame = frame
						self.__MarkedFrame.IGAS_STUDIO_MARKER.MaskColor.Visible = false
						for _, d in ipairs(_Dir) do
							self.__MarkedFrame.IGAS_STUDIO_MARKER["Marker_"..d].Visible = true
						end
						self:Fire("OnSelected", frame)
					end
				elseif self.__MarkedFrame then
					self.__MarkedFrame.IGAS_STUDIO_MARKER.MaskColor.Visible = false
					for _, d in ipairs(_Dir) do
						self.__MarkedFrame.IGAS_STUDIO_MARKER["Marker_"..d].Visible = false
					end
					self.__MarkedFrame = nil
					self:Fire("OnSelected", nil)
				end
			end,

			["Get"] = function(self)
				return self.__MarkedFrame
			end,

			["Type"] = _PropertyType.Object,
		},
	}

	local function _New(parent, wList)
		if marker then
			return marker
		end

		marker = CreateFrame(_Base, nil, parent)

		marker.__WidgetList = wList
		marker.__InMarkMode = (not wList.SelectedWidget and true) or false

		return marker
	end

	-- Register
	local _WidgetInfo = {
		["WidgetName"] = _WidgetName,
		["Base"] = _Base,
		["FuncProxy"] = _FuncProxy,
		["Property"] = _Property,
		["ScriptType"] = _ScriptType,
		["New"] = _New,
	}

	_Widget[_WidgetName] = IGAS:NewWidget(_WidgetInfo)
end

